﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CApC3DEMSAttachmentManager
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EMPAPPSERVICESLib;
using EMPTYPELIBRARYLib;
using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.IO;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands;

internal class CApC3DEMSAttachmentManager : CApC3DAttachmentManager
{
  internal const int FILES_NUMBER = 2;
  internal const string ANIMATION_FIELD = "animationData";
  internal const string VIEWS_FIELD = "viewsData";
  internal const string FILE_NAME_FIELD = "fileName";
  internal const string OPERATION_CLASS = "PmOperation";
  internal const string ANIMATION_DATA_CLASS = "PmAnimationData";
  internal const string VIEWS_DATA_CLASS = "PmViewsData";
  internal const string SYSTEM_ROOT_PREFIX_REPLACEMENT = "#\\";
  internal string m_animationFile;
  internal string m_viewsFile;

  internal CApC3DEMSAttachmentManager(CApC3DRootNodeData root)
    : base(root)
  {
  }

  internal override void Initialize()
  {
    if (this.Root.PlanningNodeObject == null || !this.Root.PlanningNodeObject.HasFields)
      return;
    TxEmsImportFieldsData importFieldsData = new TxEmsImportFieldsData();
    TxObjectList txObjectList = new TxObjectList(1);
    ((Collection<ITxObject>) txObjectList).Add((ITxObject) this.Root.NodeObject);
    importFieldsData.Roots = txObjectList;
    ArrayList pathsList = new ArrayList();
    ArrayList fieldsList = new ArrayList();
    this.addPath(ref pathsList, "PmOperation", "animationData");
    this.addPath(ref pathsList, "PmOperation", "viewsData");
    this.addField(ref fieldsList, "PmAnimationData", "fileName");
    this.addField(ref fieldsList, "PmViewsData", "fileName");
    importFieldsData.addFieldsToCollect(fieldsList);
    importFieldsData.addFieldsToTraverse(pathsList);
    if (!(TxApplication.ActiveDocument.PlatformGlobalServicesProvider is TxEmsGlobalServicesProvider servicesProvider))
      return;
    TxEmsPlatformContext newContext = servicesProvider.GetNewContext();
    servicesProvider.ImportFields((TxPlatformImportFieldsData) importFieldsData, newContext);
    TxPlanningObjectFieldData[] fields = servicesProvider.GetFields(newContext);
    if (fields.Length != 2)
      return;
    foreach (TxPlanningObjectFieldData planningObjectFieldData in fields)
    {
      foreach (TxPlanningFieldData planningFieldData in planningObjectFieldData.FieldsData)
      {
        if (planningFieldData.FieldName == "fileName")
        {
          if (planningObjectFieldData.PlanningObject.IsDerivedFromPlanningType("PmAnimationData"))
            this.m_animationFile = this.GetFullPath(planningFieldData.FieldValue as string);
          else if (planningObjectFieldData.PlanningObject.IsDerivedFromPlanningType("PmViewsData"))
          {
            this.m_viewsFile = this.GetFullPath(planningFieldData.FieldValue as string);
            this.InitializeReader(this.m_viewsFile);
          }
        }
      }
    }
  }

  internal override bool DoesHave3D => this.m_animationFile != null;

  internal override void Set3D(
    string newU3DFile,
    string newJSFile,
    string newViewDataFile,
    string newViewNodesFile)
  {
    EmpContext empContext;
    empContext.sessionId = TxApplication.ActiveDocument.eMSSessionId;
    EmpObjectKey[] empObjectKeyArray = new EmpObjectKey[2];
    string[] strArray1 = new string[2];
    string[] strArray2 = new string[2];
    int internalId = (this.Root.PlanningNodeObject.PlatformServicesProvider as ITxEmsServicesProvider).InternalId;
    empObjectKeyArray[0] = new EmpObjectKey();
    empObjectKeyArray[0].objectId = internalId;
    strArray1[0] = "animationData";
    strArray2[0] = newU3DFile;
    empObjectKeyArray[1] = new EmpObjectKey();
    empObjectKeyArray[1].objectId = internalId;
    strArray1[1] = "viewsData";
    strArray2[1] = newViewDataFile;
    ((IEmpNodesServices) new EmpNodesServicesClass()).CreateExternalDocuments(ref empContext, ref empObjectKeyArray, ref strArray1, ref strArray2);
    try
    {
      File.Delete(this.m_animationFile);
      File.Delete(this.m_viewsFile);
    }
    catch (Exception ex)
    {
      TxApplication.LogWriter.WriteExceptionLine(ex);
    }
  }

  private string GetRelativePath(string path)
  {
    return path.Replace(TxApplication.SystemRootDirectoryNormalized, "#\\");
  }

  private string GetFullPath(string path)
  {
    return path.Replace("#\\", TxApplication.SystemRootDirectoryNormalized);
  }

  private void addPath(ref ArrayList pathsList, string className, params string[] pathNames)
  {
    TxClassFieldsTraversalData fieldsTraversalData = new TxClassFieldsTraversalData();
    ArrayList arrayList = new ArrayList();
    foreach (string pathName in pathNames)
      arrayList.Add((object) pathName);
    fieldsTraversalData.ClassName = className;
    fieldsTraversalData.FieldNames = arrayList;
    pathsList.Add((object) fieldsTraversalData);
  }

  private void addField(ref ArrayList fieldsList, string className, params string[] fieldNames)
  {
    TxClassFieldsCollectionData fieldsCollectionData = new TxClassFieldsCollectionData();
    ArrayList arrayList = new ArrayList();
    foreach (string fieldName in fieldNames)
    {
      if (!arrayList.Contains((object) fieldName))
        arrayList.Add((object) fieldName);
    }
    fieldsCollectionData.ClassName = className;
    fieldsCollectionData.FieldNames = arrayList;
    fieldsList.Add((object) fieldsCollectionData);
  }
}
